package com.yuntsg.ruiijn.paperana.test;

import cn.hutool.core.collection.ListUtil;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.core.bulk.BulkOperation;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import com.yuntsg.ruiijn.databse_search.entity.FileDetailsTxt;
import com.yuntsg.ruiijn.paperana.utils.esutils.EsServerEntityTemp;
import com.yuntsg.ruiijn.paperana.utils.esutils.IndexConfig;
import common.extractText.Tools;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static com.yuntsg.ruiijn.paperana.test.FirstStart.createTextIndexLocal;

@Slf4j
public class IkChina {

    @SneakyThrows
    public static void main(String[] args) {
        //  测试中文分词器检索测试
        ElasticsearchClient esClient = getEsClient();
        String index = IndexConfig.SELF_TETX_INDEX;
        createTextIndexLocal(esClient, index);
        // 进行入库
        String directory = "D:\\Users\\55419\\Desktop\\";
        String b = "国自然正文原始文件.pdf";
        List<String> strListb = Tools.pdfToTxt(directory, directory + b);
        System.out.println(strListb.size());
        List<FileDetailsTxt> pdf = new ArrayList<>();
        for (String s : strListb) {
            if (StringUtils.isNotEmpty(s)) {
                FileDetailsTxt fileDetails = new FileDetailsTxt();
                fileDetails.setTxtContent(s);
                pdf.add(fileDetails);
            }
        }


        log.info("pdf 数量+" + pdf.size());

        dealManualDataLocal(index, pdf, esClient, 1, 1);


        log.info("入库完成");

    }

    public static ElasticsearchClient getEsClient() {
        // 创建低级客户端
        RestClient restClient = RestClient
                .builder(new HttpHost("127.0.0.1", 9200))
                .setRequestConfigCallback(re -> {
                    re.setConnectTimeout(10000);
                    re.setSocketTimeout(150000);
                    re.setConnectionRequestTimeout(20000);
                    return re;
                })
                .setHttpClientConfigCallback(re -> {
                    re.setMaxConnTotal(1000);
                    re.setMaxConnPerRoute(1000);
                    return re;
                })
                .build();

        ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
//        // 创建API客户端
        ElasticsearchClient client = new ElasticsearchClient(transport);
        return client;
    }

    /**
     * @param pdf      解析出来的待比对文章段落
     *                 // 这里下面注释可能不准确 以数据库字段为准
     * @param file_id  记录表的主键id
     * @param table_id 用户id
     * @return
     */
    public static boolean dealManualDataLocal(String index, List<FileDetailsTxt> pdf, ElasticsearchClient client, Integer file_id, Integer table_id) {
//        log.info("入库es名称+" + index);
        List<String> strsCompare = new ArrayList<>();
        for (FileDetailsTxt fileDetails : pdf) {
            String txtContent = fileDetails.getTxtContent();
            strsCompare.add(txtContent);
        }
        // 删除存在的数据
        //进行不对然后出结果
//        EsClient.deleEsDataIndex(index);
        boolean isTrue = true;
        List<BulkOperation> bulkOperations = new ArrayList<>();
        for (String s : strsCompare) {
            EsServerEntityTemp esEntity = new EsServerEntityTemp();
            esEntity.setSentence("");
            esEntity.setDatabase_type("");
            esEntity.setStr(s);
            esEntity.setSub_project("");
            esEntity.setFile_id(file_id + "");
            esEntity.setTable_id(table_id + "");
            bulkOperations.add(new BulkOperation.Builder().create(d -> d.document(esEntity)).build());
        }
        if (bulkOperations.size() > 5000) {
            List<List<BulkOperation>> split = ListUtil.split(bulkOperations, 5000);
            for (List<BulkOperation> operations : split) {
                try {
                    client.bulk(e -> e.index(index).operations(operations));
                } catch (IOException e) {
                    isTrue = false;
                    e.printStackTrace();
                }
            }
        } else {
            try {
                client.bulk(e -> e.index(index).operations(bulkOperations));
            } catch (IOException e) {
                isTrue = false;
                e.printStackTrace();
            }
        }
        return isTrue;
    }
}
